﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
using WebFunctions;
using System.Web;
using System.IO;
using Newtonsoft.Json;


namespace AutoIndustryFiles
{
    class abbScraper : FileLinksScraper2
    {
        public abbScraper()
        {
            this.MainPage = "http://www.abb.com.cn/ProductGuide/";
            this.CompanyName = "富士电机(中国有限公司)";
            this.CompanyUrl = "http://www.abb.com.cn/ProductGuide/";
            this.ColumnName = "首页-> 产品与服务 -> 产品指南";
            //this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);

            FileLink lnk = new FileLink();
        }

        /// <summary>
        /// 
        /// </summary>        
        public override void GenerateAllLinks(string CategoryId)
        {
            string Url = @"http://www.abb.com.cn/abblibrary/DownloadCenter/?CategoryID={0}&View=Result";
            

            FileLink lnk = new FileLink();
            lnk.Id = CategoryId;//"pscat"; //pscat是总类别
            lnk.Name = "产品(57575)";
            lnk.CategoryPath = "";
            lnk.IsFileLink = false;
            lnk.Url = string.Format(Url, lnk.Id);
            FileLinks.Add(lnk);
            FileLinksToScrape.Enqueue(lnk);

            while(FileLinksToScrape.Count > 0)
            {               
                FileLink lnk2 = FileLinksToScrape.Dequeue();
                bool f = false;
                UpdateMsg("abbScraper", lnk2.Url, ref f);
                Application.DoEvents();

                HtmlDoc doc = new HtmlDoc();
                doc.LoadHtml(WebFuncs.OpenUrlEx(lnk2.Url, "UTF-8"));

                int p1 = lnk2.Name.LastIndexOf("(");
                int p2 = lnk2.Name.LastIndexOf(")");

                int cnt = 0;
                if (p1 > 0 && p2 > 0 && p1 < p2)
                    int.TryParse(lnk2.Name.Substring(p1 + 1, p2 - p1 - 1), out cnt);

                if (cnt >= 400) //有400个文档链接以上的才继续
                    GenerateSubWebLinks(doc, ref lnk2, @"//*[@id=""ctl00_csMainRegion_csContentRegion_MainArea_narrowByCategoryFilter_fil""]/li/a", null, ExtractLinkAttr);
                else
                {
                    lnk2.IsFileLink = true;
                    foreach (FileLink lnk3 in FileLinks)
                        if (lnk3.Id == lnk2.Id)
                            lnk3.IsFileLink = true;
                }
            }

            StringBuilder str = new StringBuilder();

            foreach (FileLink lnk3 in FileLinks)
                str.Append(lnk3.Id + "\t" + lnk3.Url + "\t" + lnk3.Name + "\t" + lnk.IsFileLink.ToString() + "\t" + lnk3.CategoryPath + "\t" + Environment.NewLine);

            File.WriteAllText("AllCategories.txt", str.ToString());
        }

        void OnLeafNode(HtmlAgilityPack.HtmlDocument doc_obj,ref FileLink lnkParent)
        {
            //生成文件列表
            string url2 = @"http://www140.abb.com/ABBLibraryUiWebServices/ABBLibraryDownloadSectionService.svc/WebDownloadSection/GetDocumentsByDocumentKindAndProductId?method=&categoryId={0}&languageCode=zh&countryCode=CN&clientCode=abbcom&includeSubcategories=true&documentKind=&source=ds";

            string content = WebFuncs.OpenUrlEx(string.Format(url2, lnkParent.Id), "UTF-8");

            //去掉开头结尾的括号 
            content = content.TrimStart(new char[] { '(', ' ' });
            content = content.TrimEnd(new char[] { ')', ' ', ';' });

            List<abbDoc> docs = JsonConvert.DeserializeObject<List<abbDoc>>(content);

            foreach (abbDoc doc in docs)
            {
                foreach (DocValue v in doc.value)
                {
                    //dt.Rows.Add(new object[] { v.CategoryID[0], v.DocumentLanguageTitle, "", v.TranslatedDocumentKind, v.ReleaseDate, "", v.DocumentURL });
                    FileLink lnk_new = new FileLink();
                    lnk_new.Id = v.CategoryID[0];
                    lnk_new.Name = v.DocumentLanguageTitle;
                    lnk_new.FileType = v.TranslatedDocumentKind;
                    lnk_new.ReleaseDate = v.ReleaseDate;
                    lnk_new.Url = v.DocumentURL;
                    lnk_new.IsFileLink = true;
                    lnk_new.CategoryPath = lnkParent.CategoryPath + "->" + lnkParent.Name;
                    FileLinks.Add(lnk_new);
                }
            }

        }

        void ExtractLinkAttr(HtmlAgilityPack.HtmlNode node,ref FileLink lnk)
        {
            string url1 = @"http://www.abb.com.cn/abblibrary/DownloadCenter/?CategoryID={0}&View=Result";

            string id = node.GetAttributeValue("id","");
            int i = id.IndexOf("Filter_fil_");
            if (i > 0)
                id = id.Substring(i + "Filter_fil_".Length);
            
            lnk.Id = id;
            lnk.Url = string.Format(url1,lnk.Id);
            lnk.Name = System.Web.HttpUtility.HtmlDecode(node.InnerText);
            lnk.IsFileLink = false;            
        }
        /*
        public override void GenerateAllLinks()
        {
            Queue<WebLink> q_category = new Queue<WebLink> { };
            int idcount = 1;

            WebLink wl = new WebLink();
            wl.LinkId = idcount;
            wl.ParentLinkId = 0;
            wl.LinkName = "产品";
            //wl.LinkDescription = "9AAC100673";
            wl.LinkDescription = "9AAC132691";
            wl.LinkLevel = 1;

            q_category.Enqueue(wl);

            while (q_category.Count > 0)
            {
                //打开对应类别王爷
                WebLink wl2 = q_category.Dequeue();
                WebLinks.Add(wl2);

                string html = WebFuncs.OpenUrlEx(string.Format("http://www.abb.com.cn/abblibrary/DownloadCenter/?showresultstab=true&categoryid={0}",wl2.LinkDescription), "UTF-8");//url.........

                HtmlDoc doc = new HtmlDoc();
                doc.LoadHtml(html);
                
                //扫描所有子类别,加入队列
                HtmlAgilityPack.HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(@"//ul[@id=""ctl00_csMainRegion_csContentRegion_MainArea_narrowByCategoryFilter_fil""]/li/a");
                //叶子结点
                if (hnc == null)
                {
                    //处理文档列表
                    string docsUrl = string.Format(@"http://www140.abb.com/ABBLibraryUiWebServices/ABBLibraryDownloadSectionService.svc/WebDownloadSection/GetDocumentsByDocumentKindAndProductId?method=&categoryId={0}&languageCode=zh&countryCode=CN&clientCode=abbcom&includeSubcategories=true&documentKind=&source=ds", wl2.LinkDescription);
                    string content = WebFuncs.OpenUrlEx(docsUrl, "UTF-8");
                    //去掉开头结尾的括号 
                    content.TrimStart(new char[] { '(' ,' '});
                    content.TrimEnd(new char[] { ')', ' ' ,';'});

                    List<abbDoc> docs = JsonConvert.DeserializeObject<List<abbDoc>>(content);
                    //加入所有文档到weblinks
                    foreach (abbDoc abbdoc in docs)
                    {
                        foreach (DocValue v in abbdoc.values)
                        {
                            WebLink wl_doc = new WebLink();
                            wl_doc.LinkId = idcount+1;
                            wl_doc.LinkLevel = wl2.LinkLevel + 1;
                            wl_doc.LinkName = v.DocumentLanguageTitle;
                            wl_doc.LinkUrl = v.DocumentURL;
                            wl_doc.LinkType = v.RepresentationFileNameSuffix;
                            
                            WebLinks.Add(wl_doc);
                            idcount++;
                        }

                    }
                }

                else
                foreach (HtmlAgilityPack.HtmlNode node in hnc)
                {
                    string sub_category_id = node.GetAttributeValue("id","");
                    sub_category_id = sub_category_id.Substring(sub_category_id.IndexOf("Filter_fil_") + 11);
                    string sub_category_name = node.InnerText;
                    WebLink wl3 = new WebLink();
                    wl3.LinkId = idcount+1;
                    wl3.LinkLevel = wl2.LinkLevel + 1;
                    wl3.LinkName = System.Web.HttpUtility.HtmlDecode(sub_category_name);
                    wl3.ParentLinkId = wl2.LinkId;
                    wl3.LinkDescription = sub_category_id;

                    q_category.Enqueue(wl3);
                    idcount++;
                }
            }
        }

        protected bool UpdateAndCancelled(string msg)
        {
            bool bCancelled = false;
            UpdateMsg("abbScraper", msg, ref bCancelled);

            Application.DoEvents();

            return bCancelled;
        }

        public void GenerateAllLinks2()
        {
            if (UpdateAndCancelled("正在获取 " + WebLinks[0].LinkUrl))
                return;

            HtmlDoc doc = new HtmlDoc();
            doc.LoadHtml(WebFuncs.OpenUrlEx(WebLinks[0].LinkUrl, "UTF-8"));

            //GenerateNextLevelLinks2(doc, WebLinks[0], @"//div[@class=""floatingGridInner""]/div[1]/div//h3/a");

            GenerateNextTwoLevelLinks(doc, WebLinks[0], @"//div[@class=""floatingGridInner""]/div[1]/div/div", "h3/a", "ul/li/a");

            GenerateNextTwoLevelLinks(doc, WebLinks[0], @"//div[@class=""floatingGridInner""]/div[2]/div/div", "h3/a", "ul/li/a");

            int k = WebLinks.Count;

            UpdateAndCancelled("已完成");
        }*/
    }
}
